package com.qf.day11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;

public class MyCollection<E> implements Collection<E> {
    private Object[] data=new Object[10];
    private Integer count=0;
    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        return count==0;
    }

    @Override
    public boolean contains(Object o) {
        for (int i = 0; i < count; i++) {
            if(o.equals(data[i])){
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {

            // 迭代器对象的 下标 从0开始，到count-1
            private int index = 0;

            @Override
            public boolean hasNext() {
                return index<count;
            }

            @Override
            public E next() {
                return (E) data[index++];
            }
        };
    }

    @Override
    public Object[] toArray() {
        return Arrays.copyOf(data,count);
    }

    @Override
    public <T> T[] toArray(T[] a) {
        System.arraycopy(data,0,a,0,count);
        return a;
    }

    @Override
    public boolean add(E e) {
        Integer countRecord=count;
        if(count==data.length){
            data = Arrays.copyOf(data, data.length * 2);
        }
       data[count]=e;
        count++;
        if(countRecord==count){
            return false;
        }else {
            return true;
        }

    }

    @Override
    public boolean remove(Object o) {
        int index =  getElementIndex(o);

        if (index == -1){
            // 元素不存在
            return false;
        }else {
            // 把index位置后面的元素依次向前移动
            System.arraycopy(data,index+1,data,index,(count-(index+1)) );
            data[count-1] = null; // 把数组最后一个元素设置为null
            count--;
            return true;
        }
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        for (Object o : c) {
            // 只要有任何一个元素不再当前集合中，直接 return false
            if (!contains(o)) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        for(E e:c){
            add(e);
        }
        return true;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        boolean flag = false;

        for (Object o : c) {
            if (remove(o)) {
                flag = true; // 当前集合已经成功删除一个元素，集合变化了
            }
        }

        return flag;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        //1. 遍历当前集合，判断应该删除的元素有哪些，把应该删除的元素放入一个集合中
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            if (!c.contains(data[i])) {
                // 添加到删除集合中
                list.add(data[i]);
            }
        }

        //2. 删除上得到的集合中的所有元素
        return removeAll(list);
    }

    @Override
    public void clear() {
        //数组元素清除掉
        data=new Object[10];
        //把count=0
        count=0;
    }
    /**
     * 获取 元素再数组中的下标
     * @param o
     * @return -1说明元素不存在
     */
    private int getElementIndex(Object o) {

        for (int i = 0; i < count; i++) {
            if (data[i].equals(o)) {
                return i;
            }
        }

        return -1;
    }
    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[ ");

        for (int i = 0; i < count; i++) {
            stringBuffer.append(data[i]+" ");
        }

        stringBuffer.append("]");
        return stringBuffer.toString();
    }
}
